//
//  CMDBRegisterUseNameTF.swift
//  CMDB
//
//  Created by 王磊 on 05/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

class CMDBRegisterUseNameTF: CMDBSubRegisterTF {
    
    internal var addBtn: UIButton = UIButton()
    
    internal var isAddBtnHidden: Bool = false {
        
        willSet {
            
            addBtn.hidden = newValue
            
        }
    }
    
    internal var isAddBtnSelected: Bool = false {
        
        willSet {
            
            addBtn.selected = newValue
        }
    }
}
extension CMDBRegisterUseNameTF {
    
    override func addOwnSubviews() {
        super.addOwnSubviews()
        
        addSubview(addBtn)
    }
    override func configOwnSubviews() {
        super.configOwnSubviews()
        
        backgroundColor = UIColor.whiteColor()
        
        addBtn.setBackgroundImage(UIImage(named: "reg_name_add"), forState: .Normal)
        
        addBtn.setBackgroundImage(UIImage(named: "reg_name_delete"), forState: .Selected)
        
        addBtn.selected = false
        
        addBtn.setEnlargeEdge(10, right: 5, bottom: 10, left: 20)
    }
}
extension CMDBRegisterUseNameTF {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let image = UIImage(named: "reg_name_delete")
        
        addBtn.snp_makeConstraints { (make) in
            
            make.right.equalTo(-5)
            
            make.centerY.equalTo(5)
            
            make.width.equalTo(image!.size.width)
            
            make.height.equalTo(image!.size.height)
        }
    }
}
private var topNameKey = "topNameKey"

private var rightNameKey = "topNameKey"

private var bottomNameKey = "topNameKey"

private var leftNameKey = "topNameKey"

extension UIButton {
    
    internal func setEnlargeEdge(top: CGFloat ,right: CGFloat,bottom: CGFloat ,left: CGFloat) {
        
        objc_setAssociatedObject(self, &topNameKey, top, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
        objc_setAssociatedObject(self, &rightNameKey, right, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
        objc_setAssociatedObject(self, &bottomNameKey, bottom, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
        objc_setAssociatedObject(self, &leftNameKey, left, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
        
    }
    internal func enlargedRect() -> CGRect? {
        
        guard let topEdge = objc_getAssociatedObject(self, &topNameKey) as? CGFloat else {
            
            return bounds
        }
        guard let rightEdge = objc_getAssociatedObject(self, &rightNameKey) as? CGFloat else {
            
            return bounds
        }
        guard let bottomEdge = objc_getAssociatedObject(self, &bottomNameKey) as? CGFloat else {
            
            return bounds
        }
        guard let leftEdge = objc_getAssociatedObject(self, &leftNameKey) as? CGFloat else {
            
            return bounds
        }
        if (topEdge != 0 && rightEdge != 0 && bottomEdge != 0 && leftEdge != 0)
        {
            return CGRectMake(self.bounds.origin.x - leftEdge ,
                              self.bounds.origin.y - topEdge,
                              self.bounds.size.width + leftEdge + rightEdge,
                              self.bounds.size.height + topEdge + bottomEdge);
        }
        else
        {
            return bounds;
        }
    }
    public override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
        
        guard let rect = enlargedRect() else {
            
            return self
        }
        
        if CGRectEqualToRect(rect, bounds) {
            return super.hitTest(point, withEvent: event)
        }
        return CGRectContainsPoint(rect, point) ? self : nil;
        
    }
    
}